import requests
import urllib


def UrlOpen(url):
	data = urllib.request.urlopen(url).read()
	return data

def get_url(url, params=None, proxies=None):
    rsp = requests.get(url, params=params, proxies=proxies)
    rsp.raise_for_status()
    # rsp.encoding = 'utf-8'
    return rsp.text


def start_requests():
    url = 'https://fundapi.eastmoney.com/fundtradenew.aspx?ft=pg&sc=1n&st=desc&pi=1&pn=5000&cp=&ct=&cd=&ms=&fr=&plevel=&fst=&ftype=&fr1=&fl=0&isab='
    html = get_url(url)
    return html

def getHTMLText(url):
    header = {'User-agent': 'Googlebot'}
    try:
        r = requests.get(url, headers = header, timeout = 30)
        r.raise_for_status()
        r.encoding = 'gbk'
        # r.encoding = r.apparent_encoding
        return r.text
    except:
        return ''


def htmldown(url):
    f=codecs.open(url,"r","utf-8")   #使用codecs函数以打开的方式打开url 设置默认的编码方式为utf-8
    content=f.read()
    f.close()
    return content

def gethtml(url):
    content=urllib.request.urlopen(url).readlines() #使用urllib模块获取网页内容
    return content

def get_html(url):  
    # requests.adapters.DEFAULT_RETRIES = 5
    # s = requests.session()
    # s.keep_alive = False

    # response = requests.get(url)  
    # response.encoding = 'utf-8'  
    html = request.urlopen(url)  
    return html  

# s = get_url("http://www.17500.cn/getData/3d.TXT")
# print(s)